﻿namespace PMS.Client.FrmAdmin
{
    using AsyncFactory;
    using PMS.BLL;
    using PMS.Model;
    using System;
    using System.Collections.Generic;
    using System.ComponentModel;
    using System.Drawing;
    using System.Linq;
    using System.Runtime.CompilerServices;
    using System.Windows.Forms;

    public class FrmSetOrgan : Form
    {
        private AdminOrganAuthorityBLL autorityBLL = new AdminOrganAuthorityBLL();
        private List<AdminOrganAuthority> autorityList = null;
        private Button btnCancel;
        private Button btnOK;
        private CheckBox cbCheckChild;
        private IContainer components = null;
        private OrganBLL organBLL = new OrganBLL();
        private List<Organ> organList = null;
        private ImageList organTreeImageList;
        private TreeView treeView1;

        public FrmSetOrgan(int adminid)
        {
            this.InitializeComponent();
            this.AdminID = adminid;
            this.organList = this.organBLL.GetList();
            this.autorityList = this.autorityBLL.GetList(adminid);
            this.autorityList = (this.autorityList == null) ? new List<AdminOrganAuthority>() : this.autorityList;
            this.ReturnOrganBind(0, this.treeView1.Nodes);
            this.treeView1.Nodes[0].Expand();
        }

        private void btnCancel_Click(object sender, EventArgs e)
        {
            base.Close();
        }

        private void btnOK_Click(object sender, EventArgs e)
        {
            AsyncProcess.Process(delegate {
                this.autorityBLL.DeleteAll(this.AdminID);
                this.SaveTree(this.treeView1.Nodes);
            }, () => base.Close(), true, LoadingImageType.Strip);
        }

        private void cbCheckChild_CheckedChanged(object sender, EventArgs e)
        {
            if (this.cbCheckChild.Checked)
            {
                this.treeView1.AfterCheck += new TreeViewEventHandler(this.treeView1_AfterCheck);
            }
            else
            {
                this.treeView1.AfterCheck -= new TreeViewEventHandler(this.treeView1_AfterCheck);
            }
        }

        private void CheckChildNode(TreeNode node)
        {
            if (node.Nodes.Count > 0)
            {
                foreach (TreeNode node2 in node.Nodes)
                {
                    node2.Checked = true;
                    this.CheckChildNode(node2);
                }
            }
        }

        private void CheckParentNode(TreeNode node)
        {
            if (node.Parent != null)
            {
                node.Parent.Checked = true;
                this.CheckParentNode(node.Parent);
            }
            else
            {
                node.Checked = true;
            }
        }

        protected override void Dispose(bool disposing)
        {
            if (disposing && (this.components != null))
            {
                this.components.Dispose();
            }
            base.Dispose(disposing);
        }

        private void InitializeComponent()
        {
            this.components = new Container();
            ComponentResourceManager manager = new ComponentResourceManager(typeof(FrmSetOrgan));
            this.treeView1 = new TreeView();
            this.organTreeImageList = new ImageList(this.components);
            this.btnCancel = new Button();
            this.btnOK = new Button();
            this.cbCheckChild = new CheckBox();
            base.SuspendLayout();
            this.treeView1.BackColor = Color.FromArgb(0xa4, 0xc4, 0xeb);
            this.treeView1.BorderStyle = BorderStyle.FixedSingle;
            this.treeView1.CheckBoxes = true;
            this.treeView1.Dock = DockStyle.Top;
            this.treeView1.FullRowSelect = true;
            this.treeView1.HideSelection = false;
            this.treeView1.ImageIndex = 0;
            this.treeView1.ImageList = this.organTreeImageList;
            this.treeView1.ItemHeight = 0x10;
            this.treeView1.Location = new Point(0, 0);
            this.treeView1.Name = "treeView1";
            this.treeView1.SelectedImageIndex = 0;
            this.treeView1.Size = new Size(0x160, 0x1c1);
            this.treeView1.TabIndex = 1;
            this.organTreeImageList.ImageStream = (ImageListStreamer) manager.GetObject("organTreeImageList.ImageStream");
            this.organTreeImageList.TransparentColor = Color.Transparent;
            this.organTreeImageList.Images.SetKeyName(0, "ADMIN.png");
            this.organTreeImageList.Images.SetKeyName(1, "business.png");
            this.btnCancel.BackColor = Color.White;
            this.btnCancel.FlatStyle = FlatStyle.Popup;
            this.btnCancel.Location = new Point(0xcc, 0x1e8);
            this.btnCancel.Name = "btnCancel";
            this.btnCancel.Size = new Size(0x4b, 0x17);
            this.btnCancel.TabIndex = 9;
            this.btnCancel.Text = "关闭";
            this.btnCancel.UseVisualStyleBackColor = false;
            this.btnCancel.Click += new EventHandler(this.btnCancel_Click);
            this.btnOK.BackColor = Color.White;
            this.btnOK.FlatStyle = FlatStyle.Popup;
            this.btnOK.Location = new Point(0x4f, 0x1e8);
            this.btnOK.Name = "btnOK";
            this.btnOK.Size = new Size(0x4b, 0x17);
            this.btnOK.TabIndex = 8;
            this.btnOK.Text = "保存";
            this.btnOK.UseVisualStyleBackColor = false;
            this.btnOK.Click += new EventHandler(this.btnOK_Click);
            this.cbCheckChild.AutoSize = true;
            this.cbCheckChild.Location = new Point(0x4f, 0x1cb);
            this.cbCheckChild.Name = "cbCheckChild";
            this.cbCheckChild.Size = new Size(0x60, 0x10);
            this.cbCheckChild.TabIndex = 10;
            this.cbCheckChild.Text = "自动选中下级";
            this.cbCheckChild.UseVisualStyleBackColor = true;
            this.cbCheckChild.CheckedChanged += new EventHandler(this.cbCheckChild_CheckedChanged);
            base.AutoScaleDimensions = new SizeF(6f, 12f);
            base.AutoScaleMode = AutoScaleMode.Font;
            this.BackColor = Color.FromArgb(0xa4, 0xc4, 0xeb);
            base.ClientSize = new Size(0x160, 0x20b);
            base.Controls.Add(this.cbCheckChild);
            base.Controls.Add(this.btnCancel);
            base.Controls.Add(this.btnOK);
            base.Controls.Add(this.treeView1);
            base.FormBorderStyle = FormBorderStyle.FixedSingle;
            base.Icon = (Icon) manager.GetObject("$this.Icon");
            base.MaximizeBox = false;
            base.MinimizeBox = false;
            base.Name = "FrmSetOrgan";
            base.StartPosition = FormStartPosition.CenterScreen;
            this.Text = "用户机构权限分配";
            base.ResumeLayout(false);
            base.PerformLayout();
        }

        private void ReturnOrganBind(int OrganID, TreeNodeCollection treeNodes)
        {
            List<Organ> list = this.organList.FindAll(delegate (Organ f) {
                int? parentID = f.ParentID;
                int organID = OrganID;
                return (parentID.GetValueOrDefault() == organID) && parentID.HasValue;
            }).ToList<Organ>();
            if (list.Count > 0)
            {
                TreeNode node = null;
                using (List<Organ>.Enumerator enumerator = list.GetEnumerator())
                {
                    Predicate<AdminOrganAuthority> match = null;
                    Organ organ;
                    while (enumerator.MoveNext())
                    {
                        organ = enumerator.Current;
                        node = new TreeNode {
                            Text = organ.OrganName,
                            ImageIndex = organ.IsOffice - 1,
                            SelectedImageIndex = organ.IsOffice - 1,
                            Name = organ.OrganID.ToString(),
                            Tag = organ.LevelNo
                        };
                        if (match == null)
                        {
                            match = f => ((f.AdminID == this.AdminID) && (f.OrganID == organ.OrganID)) && (f.IsAuthority == 1);
                        }
                        node.Checked = this.autorityList.Exists(match);
                        node.Expand();
                        treeNodes.Add(node);
                        this.ReturnOrganBind(organ.OrganID, node.Nodes);
                    }
                }
            }
        }

        private void SaveTree(TreeNodeCollection nodes)
        {
            AdminOrganAuthority model = null;
            foreach (TreeNode node in nodes)
            {
                if (node.Checked && (node.Tag.ToString().Trim() != ""))
                {
                    model = new AdminOrganAuthority {
                        OrganID = int.Parse(node.Name),
                        AdminID = this.AdminID,
                        IsAuthority = 1
                    };
                    this.autorityBLL.Add(model);
                    this.SaveUnAuthorityOrgan(node);
                }
                this.SaveTree(node.Nodes);
            }
        }

        private void SaveUnAuthorityOrgan(TreeNode node)
        {
            if (node.Parent != null)
            {
                AdminOrganAuthority authority2 = new AdminOrganAuthority {
                    AdminID = this.AdminID,
                    IsAuthority = 0,
                    OrganID = int.Parse(node.Parent.Name)
                };
                AdminOrganAuthority model = authority2;
                this.autorityBLL.AddIFNotExits(model);
                this.SaveUnAuthorityOrgan(node.Parent);
            }
        }

        private void treeView1_AfterCheck(object sender, TreeViewEventArgs e)
        {
            if (e.Node.Checked)
            {
                this.CheckChildNode(e.Node);
            }
            else
            {
                this.UncheckNode(e.Node);
            }
        }

        private void UncheckNode(TreeNode node)
        {
            if (node.Nodes.Count > 0)
            {
                foreach (TreeNode node2 in node.Nodes)
                {
                    node2.Checked = false;
                    this.UncheckNode(node2);
                }
            }
        }

        private void UncheckParentNode(TreeNode node)
        {
            int num = 0;
            if (node.Parent != null)
            {
                foreach (TreeNode node2 in node.Parent.Nodes)
                {
                    if (!node2.Checked)
                    {
                        num++;
                    }
                }
                if (node.Parent.Nodes.Count == num)
                {
                    node.Parent.Checked = false;
                    this.UncheckParentNode(node.Parent);
                }
            }
        }

        private int AdminID { get; set; }
    }
}

